var vm = new Vue({
   el: "#app",
   data: {
       q: {
           gzhCode: "",
           status: "",
       },
       showList: true,
       title: null,
       slideshowinfo: {},
   },
   methods: {
       query: function () {
           vm.reload();
       },
       add: function () {
           vm.showList = false;
           vm.slideshowinfo = {};
           vm.title = "添加";
           vm.slideshowinfo.gzhCode = "";
           vm.slideshowinfo.status = 1;
           vm.slideshowinfo.sort = 0;
       },
       update: function (id) {
           vm.showList = false;
           vm.title = "修改";

           $.get(baseURL+"sys/bslideshowinfo/info/"+id, function (r) {
                if (r.code == 0) {
                    vm.slideshowinfo = r.slideshowInfo;
                } else {
                    alert(r.msg);
                }
           })
       },
       saveOrUpdate: function () {
           if (!vm.slideshowinfo.gzhCode || vm.slideshowinfo.gzhCode == "") {
               alert("请选择公众号！");
               return;
           }
           if (!vm.slideshowinfo.imgUrl) {
               alert("图片不能为空！");
               return;
           }
           if (!vm.slideshowinfo.connectUrl) {
               alert("链接地址不能为空！");
               return;
           }
           if (!vm.slideshowinfo.sort) {
               vm.slideshowinfo.sort = 0;
           }

           let url = vm.slideshowinfo.id ? "sys/bslideshowinfo/update" : "sys/bslideshowinfo/save";
           $.ajax({
               type: "POST",
               url: baseURL + url,
               dataType: "json",
               contentType: "application/json;charset=utf-8",
               data: JSON.stringify(vm.slideshowinfo),
               success: function (r) {
                    if (r.code == 0) {
                        alert("保存成功");
                        vm.reload();
                    } else {
                        alert(r.msg);
                    }
               }
           })
       },
       reload: function () {
           vm.showList = true;
           var page = $("#jqGrid").jqGrid('getGridParam', 'page');
           $("#jqGrid").jqGrid('setGridParam', {
               postData: {"gzhCode": vm.q.gzhCode, "status": vm.q.status},
               page: page
           }).trigger("reloadGrid");
       }
   }
});

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + "sys/bslideshowinfo/list",
        datatype: "json",
        async: false,
        colModel: [
            {label: "ID", name: "id", index: "id", align: "center", width: 60, key: true, formatter: function (value,options,rowData) {
                    return "<a onclick='vm.update("+value+")'>"+value+"</a>"
                }
            },
            {label: "图片", name: "imgUrl", index: "imgUrl", align: "center", width: 60, formatter: function (value,options,rowData) {
                    return "<img src='"+value+"' height='60px' />"
                }
            },
            {label: "链接地址", name: "connectUrl", index: "connectUrl", align: "center", width: 80},
            {label: "排序", name: "sort", index: "sort", align: "center", width: 80},
            {label: "公众号标识", name: "gzhCode", index: "gzh_code", align: "center", width: 60, hidden: true},
            {label: "公众号名称", name: "gzhName", index: "gzhName", align: "center", width: 80},
            {label: "状态", name: "status", index: "status", align: "center", width: 60, formatter: function (value,options,rowData) {
                    if (value == 0) {
                        return "<p style='color: red'>停用</p>"
                    } else if (value == 1) {
                        return "<p>正常</p>"
                    }
                }
            },
            {label: "创建时间", name: "createTime", index: "create_time", align: "center", width: 80},
            {label: "修改时间", name: "modifyTime", index: "modify_time", align: "center", width: 80}
        ],
        viewrecords: true,
        height: 520,
        rowNum: 15,
        rowList: [15, 30, 50],
        rowNumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page: "page",
            rows: "limit",
            order: "order"
        },
        postData: {"gzhCode": vm.q.gzhCode, "status": vm.q.status},
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    $.ajax({
        type: "POST",
        async:false,
        url: baseURL + 'sys/buserinfo/getUserGzhList',
        contentType: "application/json",
        success: function(r){
            if(r.code==0){
                //vm.bbrandinfo=r.brandInfo;
                $("#selectUserGzhList").append(
                    "<option value=''>全部平台</option>");
                $("#gzhCode").append(
                    "<option value=''>请选择</option>");
                for(var i=0;i<r.userGzhList.length;i++){
                    $("#selectUserGzhList").append(
                        "<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
                    $("#gzhCode").append(
                        "<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
                }
            }
        }
    });
})